package com.arvato.minipro.controller.front.member;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.arvato.minipro.bean.bean_do.user.EcUserCardDo;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.user.CardHistoryVo;
import com.arvato.minipro.bean.bean_vo.user.EcUserCardVo;
import com.arvato.common.https.WxPublicAccountRequestUtil;
import com.arvato.minipro.controller.BaseController;
import com.arvato.minipro.service.user.CardService;

/**
 * 
 * @author: smallking
 * 
 * @Descibe:处理微信公众号会员的Controller
 * 微信卡卷开发一定要熟读微信卡卷相关文档
 * 注意点：
 * 1.会员卡，代金卷等卡卷类型要知道其有什么区别。
 * 2.激活类型三种(接口激活,一键激活,自动激活)的区别
 * 3.微信的开发者文档写的都是针对公众号浏览器的环境，尤其是开卡组件
 * 开卡组件放到小程序里面，会有不同的表现类型
 * 4.微信小程序对网页的支持很弱，如果不是微信说了开放小程序的网页能力
 * 就不要想者小程序配合着网页来做数据交互
 * 
 * @Date: 2018年4月16日  下午2:24:05
 */
@Controller
@RequestMapping(value="/pubaccount/member")
public class MemberController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(MemberController.class);
	
	public static final String CARD_ID = "ppY5mwYX7shq_ugynPOCkpzttopI";
	
	@Autowired
	private WxPublicAccountRequestUtil wxPublicAccountRequestUtil;
	
	@Autowired
	private CardService cardService;
	
	
	/**
	 * 根据userId查询用户的会员详情
	 * 首先在自己的本地库查询 如果本地库没有的话 就以openid为参数发送hhtps请求微信服务器
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/info")
	public Result getMemberInfo(String userId) {
		return cardService.memberInfo(userId);
	}
	
	/**
	 * 修改用户的会员积分
	 * @param userId
	 * @param addBonus
	 * @param recordBonus
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public Result updateBonus(String userId, int addBonus, String recordBonus) {
		return cardService.updateBonus(userId, addBonus, recordBonus);
	}
	
	/**
	 * 接收一张会员卡的code和userId 存储在数据库，将用户和会员卡关联起来
	 * @param req
	 * @param cardDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/code/insert")
	public Result codeInsert(HttpServletRequest req, EcUserCardDo cardDo) {
		Result result = new Result(false);
		logger.info("插入code和userId的关联关系" + cardDo.toString());
		// 先确定之前没有此关系
		Query query = new Query();
		query.put("userId", cardDo.getUserId());
		List<EcUserCardVo> cardList = cardService.listCardByQuery(query);
		if(cardList != null && cardList.size() >= 1) {
			// 当前本地库存在此userId对应的code
			result.setCode(501);
			result.setMsg("当前code已存在，无需插入");
			return result;
		}
		// 插入code
		cardDo.setCardIdWx(CARD_ID);
		try {
			int row = cardService.insert(cardDo);
			if(row == 1) {
				result.setResult(true);
				// 成功之后调用激活卡片接口
				wxPublicAccountRequestUtil.activeCard(cardDo.getCode());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 根据userId查询用户的积分流水记录
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/history")
	public Result cardHistory(String userId) {
		Result result = new Result(true);
		Query query = new Query();
		query.put("userId", userId);
		List<CardHistoryVo> listHistoryByQuery = cardService.listHistoryByQuery(query);
		result.setData(listHistoryByQuery);
		return result;
	}
	
	/**
	 * 查询一个张卡卷是否可以被核销
	 * @param code 需要核销的卡卷的code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/consume/info")
	public Result consumeCodeInfo(String code) {
		return wxPublicAccountRequestUtil.consumeCodeInfo(code);
	}
	
	
	/**
	 * 设置会员卡的开卡字段
	 * @param req
	 * @param cardId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/card/column")
	public Result setCardColumn(HttpServletRequest req, String cardId) {
		return wxPublicAccountRequestUtil.setCardColumn(cardId);
	}
	
	/**
	 * 设置会员卡为跳转型开卡
	 * @param req
	 * @param cardId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/card/callback")
	public Result setCardJump(HttpServletRequest req, String cardId) {
		return wxPublicAccountRequestUtil.setCardJump(cardId);
	}
	
	/**
	 * 设置点击微信公众号的会员卡跳转到小程序
	 * @param req
	 * @param cardId
	 * @param miniproId gh_311e2ab7b4df
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/card/jump")
	public Result setMemberToMinipro(HttpServletRequest req, String cardId, String miniproId) {
		return wxPublicAccountRequestUtil.setMemberToMinipro(cardId, miniproId);
	}
	/**
	 * 激活会员卡
	 * @param req
	 * @param code 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/card/active")
	public Result setMemberToMinipro(HttpServletRequest req, String code) {
		return wxPublicAccountRequestUtil.activeCard(code);
	}
	/**
	 * 核销一个用户的卡卷
	 * @param code 需要核销的卡卷的code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/consume")
	public Result consumeCard(String code) {
		return wxPublicAccountRequestUtil.consumeCard(code);
	}
	
	/**
	 * 请求公众号会员卡开卡插件的拉起参数
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/card/param")
	public Result getMemberCardParam(HttpServletRequest req, String cardId) {
		return wxPublicAccountRequestUtil.getMemberCardParam(cardId);
	}
	
	/**
	 * 接受一个公众号的openId，并且查出他的unionid，插入数据库
	 * @param req
	 * @param openId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/openid/insert")
	public Result openIdInsert(HttpServletRequest req, String openId) {
		logger.info("开卡成功回调，插入openId");
		return wxPublicAccountRequestUtil.saveUnionId(openId, 2);
	}
	
	/**
	 * 得到欧唯特公众号的所有粉丝，并且将其对应的openid unionid存储到数据库
	 * @param req
	 * @param cardId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/fans/list")
	public Result getFansUsers(HttpServletRequest req, String cardId) {
		return wxPublicAccountRequestUtil.getFansUsers();
	}
	
	
}
